<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>


  <script>
    // 统计字符串中每个字符的个数？以对象的形式返回
    // var str = 'abcdacbabcbababcbabcabd'
    // 结果
    // { a:5， b: 出现次数, ...}

    var str = 'abcdacbabcbababcbabcabd'
    var arr = str.split("") //先转化为数组
    // console.log(arr);
    obj = arr.reduce(function (pre, cur) {
      if (cur in pre) {
        pre[cur] += 1;
      } else {
        pre[cur] = 1;
      }
      return pre
    }, {})
    console.log(obj);

    let newObj = {}
    for (var k in obj) {
      // console.log(k + "的出现次数为" + obj[k]);
      newObj[k] = "出现次数为" + obj[k]
    }
    console.log(newObj);
  </script>
</body>

</html>